package com.soft.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import com.soft.admin.common.ElementConst;
import com.soft.dao.ChannelDao;
import com.soft.dao.TeamDao;
import com.soft.front.dto.FTeamDTO;
import com.soft.front.dto.LocationDTO;
import com.soft.model.Channel;
import com.soft.model.Team;

@SuppressWarnings("unchecked")
@Component
public class TeamDaoImpl extends BaseDaoImpl implements TeamDao {
	
	private ChannelDao channelDao;
	
	@Autowired
	public void setChannelDao(ChannelDao channelDao) {
		this.channelDao = channelDao;
	}

	//	@Cacheable(value = "TEAM_INFO_CACHE", key = "#id")
	public Team get(Integer id) {
		return (Team)this.get(Team.class, id);
	}
	
	@CacheEvict(value = "TEAM_INFO_CACHE", allEntries = true)
	public void addOrEdit(Team team) {
		this.saveOrUpdate(team);
	}
	
	@CacheEvict(value = "TEAM_INFO_CACHE", allEntries = true)
	public void remove(Team team) {
		this.delete(team);
	}

	@Cacheable(value = "TEAM_INFO_CACHE", key = "#index + 'getLst'")
	public Map<String, Object> getLst(String index) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		//获取栏目信息
		Channel chan = this.channelDao.get(ElementConst.SZDWCONST);
		Channel indexChannel = this.channelDao.get(index);
		//获取相对应信息.
		List<Team> teamLst = this.findAll(Team.class, new Order[]{Order.desc("level"), Order.desc("releaseDate")}, 
				Restrictions.eq("state", 1), Restrictions.eq("channel.id", indexChannel.getId()));
		List<FTeamDTO> teamDTO = new ArrayList<FTeamDTO>();
		for (Team team : teamLst) {
			FTeamDTO dto = new FTeamDTO();
			BeanUtils.copyProperties(team, dto);
			dto.setUrl(ElementConst.SZDWCONST + "/" + team.getChannel().getNamespace() + "/" + team.getId() + ".jhtml");
			teamDTO.add(dto);
		}
		resMap.put("teams", teamDTO);
		resMap.put("url", ElementConst.SZDWCONST + "/index.jhtml");
		resMap.put("title", chan.getName());
		//位置定位
		List<LocationDTO> locationDTO = new ArrayList<LocationDTO>();
		LocationDTO doo = new LocationDTO();
		doo.setName(chan.getName());
		doo.setUrl(ElementConst.SZDWCONST + "/index.jhtml");
		locationDTO.add(doo);
		if (!"".equals(index)) {
			LocationDTO doo1 = new LocationDTO();
			doo1.setName(indexChannel.getName());
			doo1.setUrl(ElementConst.SZDWCONST + "/" + index + "/index.jhtml");
			locationDTO.add(doo1);
			resMap.put("url", ElementConst.SZDWCONST + "/" + index + "/index.jhtml");
			resMap.put("title", indexChannel.getName());
		}
		resMap.put("locations", locationDTO);
		return resMap;
	}

	@Cacheable(value = "TEAM_INFO_CACHE", key = "#id + #index + 'get'")
	public Map<String, Object> get(String index, Integer id) {
		//返回结果
		Map<String, Object> resMap = new HashMap<String, Object>();
		Team team = (Team)this.get(id);
		resMap.put("title", "师资队伍介绍");
		resMap.put("name", team.getName());
		resMap.put("txt", team.getTxt());
		resMap.put("pic", team.getPic());
		resMap.put("professional", team.getProfessional());
		resMap.put("study", team.getStudy());
		resMap.put("work", team.getWork());
		//位置定位
		Channel channel = team.getChannel();
		Channel chan = this.channelDao.get(ElementConst.SZDWCONST);
		List<LocationDTO> locationDTO = new ArrayList<LocationDTO>();
		LocationDTO doo = new LocationDTO();
		doo.setName(chan.getName());
		doo.setUrl(ElementConst.SZDWCONST + "/index.jhtml");
		locationDTO.add(doo);
		LocationDTO doo1 = new LocationDTO();
		doo1.setName(channel.getName());
		doo1.setUrl(ElementConst.SZDWCONST + "/" + channel.getNamespace() + "/index.jhtml");
		locationDTO.add(doo1);
		resMap.put("locations", locationDTO);
		LocationDTO doo2 = new LocationDTO();
		doo2.setName("正文");
		doo2.setUrl(ElementConst.SZDWCONST + "/" + channel.getNamespace() + "/" + id + ".jhtml");
		locationDTO.add(doo2);
		return resMap;
	}

}
